package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSetingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderSetting")
public class OrderSetingController {
    @Reference
    OrderSetingService orderSetingService;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            List<String[]> strings = POIUtils.readExcel(excelFile);

            List<OrderSetting> orderSettingList = new ArrayList<>();
            for (String[] string : strings) {
                String orderDate = string[0];
                String number = string[1];
                OrderSetting orderSetting = new OrderSetting(new Date(orderDate), Integer.parseInt(number));
                orderSettingList.add(orderSetting);
            }
            orderSetingService.add(orderSettingList);
            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS,orderSettingList);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }
    @RequestMapping("/findreservationsByDate")
    public Result findreservationsByDate(String date){
        try {
            //这里的泛型不采用OrderSetting，是因为前端需要的数据结构与这里的泛型不采用OrderSetting封装的数据不一致，因此需要进行重新组装
            List<Map> orderSettingList = orderSetingService.findreservationsByDate(date);
            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS,orderSettingList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

    }

    @RequestMapping("/editNumber")
    public Result editNumber(@RequestBody OrderSetting orderSetting ){
        try {
            orderSetingService.editNumber(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }

    }
}
